/*
 * File:   RoosterMenu.c
 * Author: Twistx77
 *
 * Created on 6 de diciembre de 2011, 16:58
 */
#include "RoosterMenu.h"

uint8_t myGLCDBuffer[(GLCD_WIDTH*GLCD_HEIGHT) / 8];
uint8_t globalState = 0, optionSelectionState = 0, timeModifyState = 0, alarmModifyState = 0, optionSelectionIsActive = 0;;
rtccTime modifyTime, modifyAlarm;



struct sGLCD myGLCD = {
    GLCD_WIDTH, GLCD_HEIGHT,
    myGLCDBuffer,
    {
        {
            { 7, &PORTA, &TRISA, &LATA},
            { 0, &PORTA, &TRISA, &LATA},
            { 2, &PORTA, &TRISA, &LATA},
            { 1, &PORTA, &TRISA, &LATA},
            { 0, &PORTC, &TRISC, &LATC}
        },
        {
            { 8, &PORTA, &TRISA, &LATA},
            { 0, &PORTA, &TRISA, &LATA},
            { 2, &PORTA, &TRISA, &LATA},
            { 1, &PORTA, &TRISA, &LATA},
            { 0, &PORTC, &TRISC, &LATC}
        }
    }
};


void initMenu(void) {
    GLCDOpen(&myGLCD);
}

void timeUnitModify(int8_t increment, uint8_t timeOrAlarm, uint8_t unitModifyState){
		switch (unitModifyState){
					case 0:
						if (timeOrAlarm)
							modifyTime.f.sec += increment;
						else 
							modifyAlarm.f.sec += increment;
					break;
					case 1: 
						if (timeOrAlarm)
							modifyTime.f.min += increment;
						else 
							modifyAlarm.f.min += increment;
					break;
					case 2: 
						if (timeOrAlarm)
							modifyTime.f.hour += increment;
						else 
							modifyAlarm.f.hour += increment;
					break;
					
					default: break;
					}
}



void clearScreen(void){
    GLCDClear(&myGLCD);
    GLCDRefresh(&myGLCD);
}
void optionSelectionScreenRefresh(uint8_t optionSelectionState) {

    GLCDRect(&myGLCD, 2, 2, myGLCD.width / 2 - 2, myGLCD.height / 2 - 2, 0, 1);
    GLCDRect(&myGLCD, myGLCD.width / 2 + 2, 2, myGLCD.width - 2, myGLCD.height / 2 - 2, 0, 1);
    GLCDRect(&myGLCD, 2, myGLCD.height / 2 + 2, myGLCD.width / 2 - 2, myGLCD.height - 2, 0, 1);
    GLCDRect(&myGLCD, myGLCD.width / 2 + 2, myGLCD.height / 2 + 2, myGLCD.width - 2, myGLCD.height - 2, 0, 1);

    switch (optionSelectionState) {

        case 0: GLCDRect(&myGLCD, 4, 4, myGLCD.width / 2 - 4, myGLCD.height / 2 - 4, 0, 1);
            break;
        case 1: GLCDRect(&myGLCD, myGLCD.width / 2 + 4, 4, myGLCD.width - 4, myGLCD.height / 2 - 4, 0, 1);
            break;
        case 2: GLCDRect(&myGLCD, 4, myGLCD.height / 2 + 4, myGLCD.width / 2 - 4, myGLCD.height - 4, 0, 1);
            break;
        case 3: GLCDRect(&myGLCD, myGLCD.width / 2 + 2, myGLCD.height / 2 + 2, myGLCD.width - 2, myGLCD.height - 2, 0, 1);
            break;

        default: break;

    }

    GLCDRect(&myGLCD, 2, 2, myGLCD.width / 2 - 2, myGLCD.height / 2 - 2, 0, 1);
    GLCDRefresh(&myGLCD);

}
void optionSelection (uint8_t pressedButton){
if (optionSelectionIsActive){
	switch (pressedButton){
		case CENTER_BUTTON:
				globalState = optionSelectionState + 2;
				buttonPressed(NONE_BUTTON);
				
				break;
		case LEFT_BUTTON:
				if (optionSelectionState > 250){
						optionSelectionState--;
						optionSelectionScreenRefresh(optionSelectionState);
					} else
						optionSelectionState = 3;				
				break;
		case RIGHT_BUTTON: 
				if (optionSelectionState<3){
					optionSelectionState++;
					optionSelectionScreenRefresh(optionSelectionState);
				} else
					optionSelectionState = 0;
				break;
		
		default: break;	
						
	}
	}else
	{
		optionSelectionIsActive = 1; 
		optionSelectionScreenRefresh(optionSelectionState);
	}
	
	
}

void alarmModifyScreenRefresh(){

	uint8_t x,y,r;
    uint8_t strW, strH;
    char str[10];

    sprintf(str,"%02d:%02d:%02d",bcdToDecimal(modifyAlarm.f.hour),bcdToDecimal(modifyAlarm.f.min),bcdToDecimal(modifyAlarm.f.sec));
    GLCDClear( &myGLCD );
    strH = Arial_Black_Normal_22.height;
    strW = GLCDTextWidth(&Arial_Black_Normal_22, str);
    GLCDText(&myGLCD, &Arial_Black_Normal_22, str, myGLCD.width / 2 - strW / 2, myGLCD.height / 2 - strH / 2 - 5, GLCD_NO_INVERT, GLCD_FILL, GLCD_COLOR_BLACK);
}

void timeModifyScreenRefresh(){

	uint8_t x,y,r;
    uint8_t strW, strH;
    char str[10];

    sprintf(str,"%02d:%02d:%02d",bcdToDecimal(modifyTime.f.hour),bcdToDecimal(modifyTime.f.min),bcdToDecimal(modifyTime.f.sec));
    GLCDClear( &myGLCD );
    strH = Arial_Black_Normal_22.height;
    strW = GLCDTextWidth(&Arial_Black_Normal_22, str);
    GLCDText(&myGLCD, &Arial_Black_Normal_22, str, myGLCD.width / 2 - strW / 2, myGLCD.height / 2 - strH / 2 - 5, GLCD_NO_INVERT, GLCD_FILL, GLCD_COLOR_BLACK);
}

void timeModify(uint8_t buttonPressed){

	switch (buttonPressed){
		case CENTER_BUTTON:
				timeModifyState++;
				break;
		case LEFT_BUTTON:
				timeUnitModify(DECREASE, TIME_MODIFY, timeModifyState);
				break;
		case RIGHT_BUTTON:
				timeUnitModify(INCREASE, TIME_MODIFY, timeModifyState);
				break;
		
		default: break;	
	}
	timeModifyScreenRefresh();
}

void alarmModify(uint8_t buttonPressed){

	switch (buttonPressed){
		case CENTER_BUTTON:
				alarmModifyState++;
				break;
		case LEFT_BUTTON:
				timeUnitModify(DECREASE, ALARM_MODIFY, alarmModifyState);
				break;
		case RIGHT_BUTTON:
				timeUnitModify(INCREASE, ALARM_MODIFY, alarmModifyState);
				break;
		
		default: break;	
	}
	alarmModifyScreenRefresh();
}



void mainScreenDraw (rtccTime presentTime, rtccDate presentDate, float presentTemperature, float presentLightLevel, float presentBatteryVoltage){
    uint8_t x,y,r;
    uint8_t strW, strH;
    char str[10];

    sprintf(str,"%02d:%02d:%02d",bcdToDecimal(presentTime.f.hour),bcdToDecimal(presentTime.f.min),bcdToDecimal(presentTime.f.sec));
    GLCDClear( &myGLCD );
    strH = Arial_Black_Normal_22.height;
    strW = GLCDTextWidth(&Arial_Black_Normal_22, str);
    GLCDText(&myGLCD, &Arial_Black_Normal_22, str, myGLCD.width / 2 - strW / 2, myGLCD.height / 2 - strH / 2 - 5, GLCD_NO_INVERT, GLCD_FILL, GLCD_COLOR_BLACK);


    sprintf(str,"%d-%02d-%02d",bcdToDecimal(presentDate.f.mday),bcdToDecimal(presentDate.f.mon),bcdToDecimal(presentDate.f.year));
    GLCDText(&myGLCD, &Mongolian_Baiti_12, str, 40, 40, GLCD_NO_INVERT, GLCD_FILL, GLCD_COLOR_BLACK);


    sprintf(str,"06:30");
    GLCDText(&myGLCD, &Mongolian_Baiti_12, str, 2, 2, GLCD_NO_INVERT, GLCD_FILL, GLCD_COLOR_BLACK);

    sprintf(str,"%.1fC",presentTemperature);
    GLCDText(&myGLCD, &Mongolian_Baiti_12, str, myGLCD.width-30, 2, GLCD_NO_INVERT, GLCD_FILL, GLCD_COLOR_BLACK);
    
    sprintf(str,"%.1fV",presentBatteryVoltage);
    GLCDText(&myGLCD, &Mongolian_Baiti_12, str, 2, myGLCD.height-15, GLCD_NO_INVERT, GLCD_FILL, GLCD_COLOR_BLACK);

    sprintf(str,"%f",presentLightLevel);
    GLCDText(&myGLCD, &Mongolian_Baiti_12, str, myGLCD.width-20, myGLCD.height-12, GLCD_NO_INVERT, GLCD_FILL, GLCD_COLOR_BLACK);

    GLCDRefresh(&myGLCD);
}

void buttonPressed (int8_t buttonPressed){

    switch(globalState){
	case 0:
			optionSelection(buttonPressed);	
            break;
        case 1:
			timeModify(buttonPressed);
			break;
			
        case 2:
			alarmModify(buttonPressed);
			break;

        default: break;

    }



}

